package com.gopay.common.rcs.dao.antiphish.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.domain.rcs.RcsAntiPhishConfGlobal;
import com.gopay.common.rcs.bean.DomainnameCheckStatus;
import com.gopay.common.rcs.bean.IllegalReqDicision;
import com.gopay.common.rcs.bean.OrderIPCheckStatus;
import com.gopay.common.rcs.bean.TimestampCheckStatus;
import com.gopay.common.rcs.bean.VerifyCodeCheckStatus;
import com.gopay.common.rcs.dao.antiphish.RcsAntiPhishingGlobalDAO;

/**
 * 防钓鱼全局配置
 * 
 * @RcsAntiPhishingGlobalDAO.java
 * @author liujy
 * @2013-7-23 下午6:40:19 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("rcsAntiPhishingGlobalDAO")
public class RcsAntiPhishingGlobalDAOImpl extends BaseDAOImpl<RcsAntiPhishConfGlobal, Serializable> implements
        RcsAntiPhishingGlobalDAO {
    protected final static Log logger = LogFactory.getLog(RcsAntiPhishingGlobalDAOImpl.class);

    private String getStr(Object obj) {
        if (null == obj){
            return "";
        }else{
            return obj.toString();
        }
    }

    @Override
    public DomainnameCheckStatus getDomainnameCheckStatus(final String custId) {
        final StringBuilder buf = new StringBuilder(512);
        buf.append(" SELECT DISTINCT 'G' TITLE,G.IS_ENABLED,'' VALUE");
        buf.append("  FROM RCS_ANTI_PHISH_CONF_GLOBAL G");
        buf.append(" WHERE G.ANTI_PHISH_TYPE = '01'");
        buf.append(" UNION ALL");
        buf.append(" SELECT DISTINCT 'E' TITLE,M.IS_ENABLED, M.LEGAL_VALUE VALUE");
        buf.append("  FROM RCS_ANTI_PHISH_CONF_CUST M");
        buf.append(" WHERE M.CUST_ID = :custId ");
        buf.append("   AND M.ANTI_PHISH_TYPE = '01' ");
        buf.append(" UNION ALL");
        buf.append(" SELECT DISTINCT 'F' TITLE,M.IS_ENABLED, M.LEGAL_VALUE VALUE");
        buf.append("  FROM RCS_ANTI_PHISH_CONF_CUST M");
        buf.append(" WHERE M.CUST_ID = :custId ");
        buf.append("   AND M.ANTI_PHISH_TYPE = '02'");
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("custId", custId);
        
        List<?> rows = findBySql(buf.toString(), map);
        DomainnameCheckStatus s = new DomainnameCheckStatus(custId);
        if (null == rows || rows.isEmpty()){
        	return s;
        }
        
        String legalValue = "";
    	for (int i = 0; i < rows.size(); i++) {
            Object[] row = (Object[]) rows.get(i);
            String title = getStr(row[0]);
            String is_enabled = getStr(row[1]);
            String value = getStr(row[2]);
            logger.info("AntiPhishingCheck-DomainnameCheckStatus-" + custId + "-TITLE:" + title
                    + ", IS_ENABLED:" + is_enabled + ", VALUE:" + value);
            if ("G".equalsIgnoreCase(title)) {// 全局
                if ("1".equals(is_enabled)) {
                    s.setGlobalOn(true);
                }
            }
            if ("E".equalsIgnoreCase(title)) {// 精确匹配:M_EXACT
                if ("1".equals(is_enabled)) {
                    s.setMerchantOn(true);
                }
                legalValue = value;// 如果全局开启，商户关闭，仍然取商户的配置。
            }
            if ("F".equalsIgnoreCase(title)) {// 模糊匹配:M_FUZZY
                if ("1".equals(is_enabled)) {
                    s.setFuzzyOn(true);
                }
                legalValue = value;// 如果全局开启，商户关闭，仍然取商户的配置。
            }
        }
        s.setLegalValue(legalValue);
        
        logger.info("AntiPhishingCheck-DomainnameCheckStatus-" + custId + "-RESULT:" + s);
        return s;
    }

    @Override
    public TimestampCheckStatus getTimestampCheckStatus(final String custId) {

        final StringBuilder buf = new StringBuilder(512);
        buf.append(" SELECT DISTINCT 'G' TITLE, G.IS_ENABLED, G.LEGAL_VALUE VALUE");
        buf.append("  FROM RCS_ANTI_PHISH_CONF_GLOBAL G");
        buf.append(" WHERE G.ANTI_PHISH_TYPE = '03'");
        buf.append(" UNION ALL");
        buf.append(" SELECT DISTINCT 'M' T, M.IS_ENABLED, M.LEGAL_VALUE VALUE");
        buf.append("  FROM RCS_ANTI_PHISH_CONF_CUST M");
        buf.append(" WHERE M.CUST_ID = :custId ");
        buf.append("   AND M.ANTI_PHISH_TYPE = '03'");
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("custId", custId);
        
        List<?> rows = findBySql(buf.toString(), map);
        TimestampCheckStatus s = new TimestampCheckStatus(custId);
        if (null == rows || rows.isEmpty()){
        	return s;
        }
        
        String legalValue = "";
        for (int i = 0; i < rows.size(); i++) {
            Object[] row = (Object[]) rows.get(i);
            String title = getStr(row[0]);
            String is_enabled = getStr(row[1]);
            String value = getStr(row[2]);
            logger.info("AntiPhishingCheck-TimestampCheckStatus-" + custId + "-TITLE:" + title
                    + ", IS_ENABLED:" + is_enabled + ", VALUE:" + value);
            if ("G".equalsIgnoreCase(title)) {// 全局
                if ("1".equals(is_enabled)) {
                    s.setGlobalOn(true);
                    legalValue = value;
                }
            }
            if ("M".equalsIgnoreCase(title)) {// 商户
                if ("1".equals(is_enabled)) {
                    s.setMerchantOn(true);
                    legalValue = value;
                }
            }
        }
        if (StringUtils.isNotBlank(legalValue)) {
            s.setLegalValue(Integer.parseInt(legalValue));
        }
        logger.info("AntiPhishingCheck-TimestampCheckStatus-" + custId + "-RESULT:" + s);
        return s;
    }

    @Override
    public OrderIPCheckStatus getOrderIPCheckStatus(final String custId) {
        final StringBuilder buf = new StringBuilder(512);
        buf.append(" SELECT DISTINCT 'G' TITLE, G.IS_ENABLED, G.ILLEGAL_REQ_DICISION VALUE");
        buf.append("  FROM RCS_ANTI_PHISH_CONF_GLOBAL G");
        buf.append(" WHERE G.ANTI_PHISH_TYPE = '04'");
        buf.append(" UNION ALL");
        buf.append(" SELECT DISTINCT 'M' T, M.IS_ENABLED, M.ILLEGAL_REQ_DICISION VALUE");
        buf.append("  FROM RCS_ANTI_PHISH_CONF_CUST M");
        buf.append(" WHERE M.CUST_ID = :custId ");
        buf.append("   AND M.ANTI_PHISH_TYPE = '04'");
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("custId", custId);
        
        List<?> rows = findBySql(buf.toString(), map);
        OrderIPCheckStatus s = new OrderIPCheckStatus(custId);
        if (null == rows || rows.isEmpty()){
        	return s;
        }
        
        String legalValue = "";
        for (int i = 0; i < rows.size(); i++) {
            Object[] row = (Object[]) rows.get(i);
            String title = getStr(row[0]);
            String is_enabled = getStr(row[1]);
            String value = getStr(row[2]);
            logger.info("AntiPhishingCheck-OrderIPCheckStatus-" + custId + "-TITLE:" + title + ", IS_ENABLED:"
                    + is_enabled + ", VALUE:" + value);
            if ("G".equalsIgnoreCase(title)) {// 全局
                if ("1".equals(is_enabled)) {
                    s.setGlobalOn(true);
                    legalValue = value;
                }
            }
            if ("M".equalsIgnoreCase(title)) {// 商户
                if ("1".equals(is_enabled)) {
                    s.setMerchantOn(true);
                    legalValue = value;
                }
            }
        }
        if (StringUtils.isNotBlank(legalValue)) {
            s.setIllegalReqDicision(IllegalReqDicision.get(legalValue));
        }
        logger.info("AntiPhishingCheck-OrderIPCheckStatus-" + custId + "-RESULT:" + s);
        return s;
    }

    @Override
    public VerifyCodeCheckStatus verifyCodeConfig(final String custId) {

        final StringBuilder buf = new StringBuilder(512);
        buf.append(" SELECT DISTINCT 'G' TITLE, G.IS_ENABLED, G.GATEWAY_CHANNEL VALUE");
        buf.append("  FROM RCS_ANTI_PHISH_CONF_GLOBAL G");
        buf.append(" WHERE G.ANTI_PHISH_TYPE = '05'");
        buf.append(" UNION ALL");
        buf.append(" SELECT DISTINCT 'M' T, M.IS_ENABLED, M.GATEWAY_CHANNEL VALUE");
        buf.append("  FROM RCS_ANTI_PHISH_CONF_CUST M");
        buf.append(" WHERE M.CUST_ID = :custId");
        buf.append("   AND M.ANTI_PHISH_TYPE = '05'");
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("custId", custId);
        
        List<?> rows = findBySql(buf.toString(), map);
        VerifyCodeCheckStatus s = new VerifyCodeCheckStatus(custId);
        if (null == rows || rows.isEmpty()){
        	return s;
        }
        
        boolean global_direct = false, global_undirect = false, mer_direct = false, mer_undirect = false;
        for (int i = 0; i < rows.size(); i++) {
            Object[] row = (Object[]) rows.get(i);
            String title = getStr(row[0]);
            String is_enabled = getStr(row[1]);
            String value = getStr(row[2]);
            logger.info("AntiPhishingCheck-VerifyCodeCheckStatus-" + custId + "-TITLE:" + title
                    + ", IS_ENABLED:" + is_enabled + ", VALUE:" + value);
            if ("G".equalsIgnoreCase(title)) {// 全局
                if ("01".equals(value) || "00".equals(value)) {
                    if ("1".equals(is_enabled)){
                        global_direct = true;
                    }
                }
                if ("02".equals(value) || "00".equals(value)) {
                    if ("1".equals(is_enabled)){
                        global_undirect = true;
                    }
                }
            }
            if ("M".equalsIgnoreCase(title)) {// 商户
                // 如果商户有配置，则以商户的为准，全局配置作废。
                global_direct = false;
                global_undirect = false;
                if ("01".equals(value) || "00".equals(value)) {
                    if ("1".equals(is_enabled)){
                        mer_direct = true;
                    }
                }
                if ("02".equals(value) || "00".equals(value)) {
                    if ("1".equals(is_enabled)) {
                        mer_undirect = true;
                    }
                }
            }
        }
        s.setDirectPayOn(global_direct || mer_direct);
        s.setUnDirectPayOn(global_undirect || mer_undirect);
        logger.info("AntiPhishingCheck-VerifyCodeCheckStatus-" + custId + "-RESULT:" + s);
        return s;
    }
}
